<template>
  <view class="login-container">
    <uni-forms ref="loginForm" :model="form" :rules="rules">
      <uni-forms-item name="username">
        <uni-easyinput v-model="form.username" placeholder="用户名" />
      </uni-forms-item>
      <uni-forms-item name="password">
        <uni-easyinput type="password" v-model="form.password" placeholder="密码" />
      </uni-forms-item>
      <button type="primary" @click="submitForm">登录</button>
    </uni-forms>
  </view>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: { required: true, message: '请输入用户名' },
        password: { required: true, message: '请输入密码' }
      }
    };
  },
  methods: {
    async submitForm() {
      try {
        await this.$refs.loginForm.validate();
        const res = await uni.request({
          url: '/agent/login',
          method: 'POST',
          data: this.form
        });
        uni.setStorageSync('token', res.data.token);
        uni.switchTab({ url: '/pages/agent/index' });
      } catch (e) {
        uni.showToast({ title: e.message, icon: 'none' });
      }
    }
  }
};
</script>
<style>
.login-container {
  padding: 20px;
}
</style>